package org.cyz.tools.controller;

import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Side;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import org.cyz.tools.common.CommonState;
import org.cyz.tools.common.util.ApplicationUtil;
import org.cyz.tools.core.ContextManager;
import org.cyz.tools.core.assembly.SystemMenu;
import org.cyz.tools.core.assembly.log.Log;
import org.cyz.tools.core.handler.ControlEventHandler;

import java.net.URL;
import java.util.ResourceBundle;

public class MainController extends ControlEventHandler implements Initializable {
    private BodyController bodyController = null;
    private NewConnectionController newConnectionController;
    @FXML
    public TreeView treeView;
    @FXML
    private Button settings;
    @FXML
    private Label title;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //放入管理器中
        ContextManager.ctrManager.addController(this.getClass().getSimpleName(), this);
        bodyController = (BodyController) ContextManager.ctrManager.getController(BodyController.class.getSimpleName());
        newConnectionController= (NewConnectionController) ContextManager.ctrManager.getController(NewConnectionController.class.getSimpleName());
        //读取版本和作者信息
        ApplicationUtil.initApplicationInfo(title);
        //初始化监听
        initListener();
    }


    private void initListener() {
        //注册菜单
        super.registerMenu(treeView, bodyController);
        //初始化右键菜单
        super.initMenu();
        //检测版本
//        super.checkVersion();
    }


    /**
     * 系统设置
     *
     * @param mouseEvent
     */
    public void settings(MouseEvent mouseEvent) {
        //显示悬浮菜单
        Platform.runLater(() -> SystemMenu.getInstance().show(settings, Side.BOTTOM, 0, 0));
    }

    /**
     * 最小化
     *
     * @param mouseEvent
     */
    public void min(MouseEvent mouseEvent) {
        ContextManager.stageManager.getStage("Main").setIconified(true);
    }


    /**
     * 退出系统
     *
     * @param mouseEvent
     */
    public void closeWin(MouseEvent mouseEvent) {
        Log.info(MainController.class.getSimpleName(),"exit system...");
        System.exit(0);
    }


    public void indexMainFunc(MouseEvent mouseEvent) {
        // 判断是否索引到了功能页
        if(CommonState.INDEX_FUNC>0){
            //索引到当前

        }
        //判断treeview是否为空
        if(null==treeView.getRoot()){
            //调用新建连接页面
            showConnForm();
        }
    }
}
